/*
 * HazardQueue.hpp
 * Author: Elias Capito, Yanick Candaux
 * PCM
 */


#ifndef HAZARDQUEUE_HPP_
#define HAZARDQUEUE_HPP_

#include "def.hpp"
#include "BoardInfos.hpp"
#include "Node.hpp"
#include <cstring>
#include <omp.h>

using namespace std;

namespace pcm {
	
	class HazardQueue {
		
	public:
		
		int m_numberThread;
		Node ***HP;
		Node ***LocalQueues;
		
		Node *Head;
		Node *Tail;
		
		HazardQueue(int _numberThread);
		bool initializeQueue();
		int size();
		void initializeHazardPointers(void);
		bool enq(BoardInfos *value, unsigned threadID);
		BoardInfos *deq(unsigned threadID);
		void freeNode(Node *node,unsigned tid);
		Node *getFreeNode(unsigned tid);
		bool isReferenced(Node *node);
		
		inline void clear(){
			Node *pred;

			for(int i=0;i<m_numberThread;i++){
				while(true){
					pred = LocalQueues[i][0];
					if (pred != NULL) {
							LocalQueues[i][0] = pred->next();
							delete pred;
					}else{
						break;
					}
				}
				delete[] HP[i];
				delete[] LocalQueues[i];
			}
			delete Tail;
		}
	protected:
		int m_size;
		
		
		
		
	private:
		
	};	
} /* namespace pcm */
#endif /* HAZARDQUEUE_HP_ */
